import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Coman from '../components/Coman.vue'
import Data from '../components/Data.vue'
import Sse from '../components/Svipsse.vue'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: 'login'
},
{
  path: '/login',
  component: Login
},
{
  path: '/home',
  component: Home,
  redirect: '/sse',
  children: [
    {
      path: '/sse',
      component: Sse
    },
    {
      path: '/coman',
      component: Coman
    },
    {
      path: '/data',
      component: Data
    }
  ]
}
]

const router = new VueRouter({
  routes
})

// 挂载路由导航首位
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从那个路径跳转过来
  // next 是一个函数 表示放行
  if (to.path === '/login' || to.path === '/login-svip') return next()
  // 先获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
